package com.hz.friends.activity;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.hz.friends.serverrequest.ALEngineConfig.CallBack;
import com.hz.friends.serverrequest.ALUserEngine;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class ChangePwdActivity extends BaseActivity implements OnClickListener {
	private EditText edt_curpwd;
	private EditText edt_newpwd;
	private EditText edt_repeatpwd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_changepwd);
		initViews();
	}

	@Override
	protected void init() {

	}

	@Override
	protected void initViews() {
		ImageButton imbtn_bar_left = (ImageButton) findViewById(R.id.imbtn_bar_left); // bar
		imbtn_bar_left.setOnClickListener(this);
		imbtn_bar_left.setVisibility(View.VISIBLE); // 左边按钮
		Button btn_bar_righ = (Button) findViewById(R.id.btn_bar_righ);
		btn_bar_righ.setText("确认");
		btn_bar_righ.setVisibility(View.VISIBLE);
		ImageButton imbtn_bar_right = (ImageButton) findViewById(R.id.imbtn_bar_right); // bar
		TextView imbtn_bar_title = (TextView) findViewById(R.id.imbtn_bar_title); // bar
		imbtn_bar_left.setImageResource(R.drawable.ic_leftback_arrow);
		btn_bar_righ.setOnClickListener(this);
		imbtn_bar_title.setText("Change password");
		imbtn_bar_right.setVisibility(View.GONE);
		
		edt_curpwd = (EditText) findViewById(R.id.edt_curpwd);
		edt_newpwd = (EditText) findViewById(R.id.edt_newpwd);
		edt_repeatpwd = (EditText) findViewById(R.id.edt_repeatpwd);

	}

	@Override
	protected void setViews() {

	}

	@Override
	protected void initEvents() {

	}

	private void sumbit() {
		String curpwd = edt_curpwd.getText().toString().trim();
		String newpwd = edt_newpwd.getText().toString().trim();
		String repeatpwd = edt_repeatpwd.getText().toString().trim();
		if (curpwd.length() == 0) {
			showToast("请输入当前密码");
			return;
		}
		if (newpwd.length() == 0) {
			showToast("请输入新密码");
			return;
		}
		if (repeatpwd.length() == 0) {
			showToast("请重复输入新密码");
			return;
		}
		if(newpwd.equals(curpwd)){
			showToast("新密码与当前密码一样,请重新输入");
			return;
		}
		
		if (!newpwd.equals(repeatpwd)) {
			showToast("两次密码输入不一致,请重新输入");
			return;
		}
		createWiatDialog("正在请求服务器");
		ALUserEngine.defauleEngine().resetPassword(curpwd, newpwd, new CallBack() {
			
			@Override
			public void done(Object object, AVException avexception) {
                  cancleWaitDialog();
                  if(avexception == null){
                	  showToast("修改密码成功");
                	  finish();
                  }else{
                	  showToast("修改密码失败");
                	  avexception.printStackTrace();
                  }
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imbtn_bar_left:
			finish();
			break;
		case R.id.btn_bar_righ:
			sumbit();
			break;
		default:
			break;
		}
	}

}
